#
#Makefile
#

#CROSS_COMPILE := i686-w64-mingw32-

LIBNAME := cobject
MAJVER := 1
MINVER := 0

PREFIX := build

SRC := $(wildcard *.c)
OBJ := $(SRC:.c=.o)

SLIB := lib$(LIBNAME).a
ifeq ($(CROSS_COMPILE), i686-w64-mingw32-)
	HOST := windows
	DLIB := lib$(LIBNAME).dll
else
	ifdef CROSS_COMPILE
		HOST := arm_linux
	else
		HOST := x86_linux
	endif
	DLIB := lib$(LIBNAME).so
endif

#CFLAGS += -g
CFLAGS += -fPIC
CFLAGS += -Wall
CFLAGS += -I./uthash
LDFLAGS += -shared

CC := $(CROSS_COMPILE)gcc
AR := $(CROSS_COMPILE)ar
RM := rm -f

.PHONY:def before print clean distclean all help

def: before print $(DLIB) $(SLIB)
before:	
	@if [ ! -d $(PREFIX)/$(HOST) ] ; then \
		mkdir -p $(PREFIX)/$(HOST) ; \
	fi

print:
	@echo "CC = $(CC)"

$(DLIB):$(OBJ)
	@echo "CC shared $@"
	@$(CC) $(CFLAGS) $^ -o $(PREFIX)/$(HOST)/$@ $(LDFLAGS)
	
$(SLIB):$(OBJ)
	@echo "AR $@"
	@$(AR) cr $(PREFIX)/$(HOST)/$@ $^

$(OBJ):%.o:%.c
	@echo "CC $@"
	@$(CC) $(CFLAGS) -c $< -o $@

clean:	
	@echo "RM *.o"
	@$(RM) *.o

distclean:clean
	@echo "RM lib$(LIBNAME)"
	@$(RM) $(PREFIX)/$(HOST)

all:def clean

help:
	@echo "make options:"
	@echo "CROSS_COMPILE --- set cross compiler, default CC=gcc"
	@echo ""
	@echo "e.g make CROSS_COMPILE=i686-w64-mingw32-[x86_64-w64-mingw32-]"
